package com.example.mvvm.base.vm.action;


import androidx.lifecycle.MutableLiveData;

import com.example.mvvm.base.data.SingleLiveEvent;

public class UIChangeObservable {
    private SingleLiveEvent<Boolean> refreshEvent;
    private SingleLiveEvent<Boolean> loadEvent;
    private SingleLiveEvent<String> dialogTipEvent;
    private MutableLiveData<Boolean> dialogShowEvent = new MutableLiveData<>();
    private SingleLiveEvent<String> shortToastEvent;
    private SingleLiveEvent<String> longToastEvent;
    private SingleLiveEvent<Object> netUiEvent;

    private <T> SingleLiveEvent<T> createLiveData(SingleLiveEvent<T> liveData) {
        if (liveData == null) {
            liveData = new SingleLiveEvent<>();
        }
        return liveData;
    }

    private <T> SingleLiveEvent<T> createLiveData(SingleLiveEvent<T> liveData, T data) {
        if (liveData == null) {
            liveData = new SingleLiveEvent<>(data);
        }
        return liveData;
    }

    public SingleLiveEvent<Object> getNetUiEvent() {
        return netUiEvent=createLiveData(netUiEvent);
    }

    public SingleLiveEvent<Boolean> getRefreshEvent() {
        return refreshEvent = createLiveData(refreshEvent);
    }

    public SingleLiveEvent<Boolean> getLoadEvent() {
        return loadEvent = createLiveData(loadEvent);
    }

    public MutableLiveData<Boolean> getDialogShowEvent() {
        return dialogShowEvent;
    }

    public SingleLiveEvent<String> getDialogTipEvent() {
        return dialogTipEvent = createLiveData(dialogTipEvent);
    }

    public SingleLiveEvent<String> getShortToastEvent() {
        return shortToastEvent = createLiveData(shortToastEvent);
    }

    public SingleLiveEvent<String> getLongToastEvent() {
        return longToastEvent = createLiveData(longToastEvent);
    }

    public void showDialog() {
        showDialog("请稍等");
    }

    public void showDialog(String tip) {
        getDialogShowEvent().postValue(true);
        setDialogTip(tip);
    }

    private void setDialogTip(String tip) {
        if (tip != null) {
            getDialogTipEvent().postValue(tip);
        }
    }

    public void hideDialog() {
        getDialogShowEvent().postValue(false);
    }

    public void showLongToast(String info) {
        if (info != null) {
            getLongToastEvent().postValue(info);
        }
    }

    public void showShortToast(String info) {
        if (info != null) {
            getShortToastEvent().postValue(info);
        }
    }

}